/**
 * Collection List
 *  -  user
 *  -  user_followups
 *  -  user_authentications
 *  -  user_community
 */
var Schema = mongoose.Schema;

/**
 * ==================== 用户 - User============================
 */
var userMongoSchema = new Schema({
    userType: {
        type: String,
        enum: ['normalAppUser', 'talent', 'moderator', 'service'],
        default: 'normalAppUser'
    },
    userName: {type: String, unique: true, sparse: true},        //唯一，空值除外
    email: {type: String, unique: true, sparse: true},            //唯一，空值除外
    mobilePhone: {type: String, unique: true, sparse: true},     //唯一，空值除外
    huanxinUserName: {type: String, unique: true},                //唯一，不能为空，环信用户名
    huanxinPassword: {type: String},                                //环信密码
    wechatId: {type: String, unique: true, sparse: true},         //唯一，空值除外，微信token
    thirdParty: [{
        id: String,
        platform: String,
        time: {type: Date, default: Date.now}
    }],
    /* 宝宝树66小区二期拼团    add 登录注册接口变更  llh  20170822   start */
    // userPassword: {type: String, required: true},                 //口令
    userPassword: {type: String},                 //口令
    /* 宝宝树66小区二期拼团    add 登录注册接口变更  llh  20170822   end */
    nickName: String,                                              //昵称
    /*  宝宝树66小区五期   add  小区大使申请 guo.xiaomin  20171121   start   */
    realName: String, // 真实姓名
    wxName: String, // 微信号
    /*  宝宝树66小区五期   add  小区大使申请 guo.xiaomin  20171121   end   */
    /*  宝宝树66小区二次改造    add   luolh  20170721   start   */
    nickUpCnt : Number,                                            // 昵称修改次数
    /*  宝宝树66小区二次改造    add   luolh  20170721   end   */
    exp: Number,                                                     //贡献度
    pinyin: String,                                                //昵称拼音
    pinyinShort: String,                                           //昵称拼音首字母
    pinyinSort: String,                                              //拼音排序
    userPortrait: {type: Schema.Types.ObjectId, ref: 'masterData_UserPortrait'},
    frozenOrNot: {type: Boolean, default: false},                   //是否激活
    frozenReason: String,
    images: [{imageURL: String}],
    maritalStatus: String,                                                     //婚姻状况
    hasChildren: {type: String, enum: ['Yes', 'No', '有', '无']},
    authenticationStatus: {type: Boolean, default: false},                 //认证状态
    school: String,                                                             //毕业院校
    birthDate: Date,                                                           //生日
    speciality: String,                                                        //擅长
    job: String,                                                                //工作
    hobbies:                                                                   //兴趣爱好
        [
            {type: Schema.Types.ObjectId, ref: 'masterData_Dictionaries'}    //关联到兴趣爱好的schema ID
        ],
    contactBook: [
        {type: Schema.Types.ObjectId, ref: 'user'}                              //通信录
    ],
    age: {type: Number, min: 10, max: 100},                                         //年龄
    gender: {type: String, enum: ['男', '女', 'Male', 'Female'], default: '女'},                 //性别
    community: {type: Schema.Types.ObjectId, ref: 'community'},               //所属小区
    cityId: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},
    communityWay: Number,                                                       //1,设置小区;2,随便逛逛
    apartmentDetail: String,                                                       //详细住址
    roomUnit: String,                                                               //单元
    roomNumber: String,                                                             //房号
    showUnit: {type: Boolean, default: true},                                     //显示单元号
    homeTown: {
        country: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},        //所属国家
        province: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},       //所属省
        city: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},           //所属市
        county: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'}          //所属县
    },
    appSetting: {
        neighborQuestionMessageOn: {type: Boolean, default: true},   //邻居问题推送
        //followupQuestionMessageOn: {type: Boolean, default: true},        //关注问题推送
        //backOfficeMessageOn: {type: Boolean, default: true},              //运营平台推送
        neighborChatMessageOn: {type: Boolean, default: true},        //邻居聊天推送
        //showMeNearOn: {type: Boolean, default: true},                     //附近显示我
        notDisturb: {
            on: {type: Boolean, default: false},
            startTime: String,
            endTime: String
        },
        vibrationOn: {type: Boolean, default: true},
        soundOn: {type: Boolean, default: true}
    },
    clientId: String,
    regChannelId: String,                                   //注册渠道id
    dateCreated: {type: Date, default: Date.now},
    dateLastModified: {type: Date, default: Date.now},
    invitationCode: String,
    invitedBy: {type: Schema.Types.ObjectId, ref: 'user'},
    openId: String,
    deviceId: String,
    loginWay: String,
    /* 宝宝树66小区二期拼团    add 登录注册接口变更  llh  20170822   start */
    loginStr: String,// 登录loginStr,不用保存数据
    encUserId: String,// 宝宝树用户id
    meitunToken: String,// 美囤token,不用保存数据
    /* 宝宝树66小区二期拼团    add 登录注册接口变更  llh  20170822   end */
    userRole: [{type: Number}],           //用户角色  1-版主 2-物业 3-雷锋团  4-创始人
    children: {
        gender: String,
        birthday: String
    },
    /* 宝宝树66小区二次改造 新老用户登录勾选新用户协议 mod wangcan 20170927 start */
    protocolVersion: String // 协议版本号
    /* 宝宝树66小区二次改造 新老用户登录勾选新用户协议 mod wangcan 20170927 end */
});
mongoose.model('user', userMongoSchema, 'user');


//===========================用户关注 userFollowup ========================================
var userFollowupSchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    category: {type: String, enum: ['question', 'activity', 'user', 'goods']},
    question: {type: Schema.Types.ObjectId, ref: 'question', index: true},                //关注的问题
    activity: {type: Schema.Types.ObjectId, ref: 'activity', index: true},               //关注的活动
    goods: {type: Schema.Types.ObjectId, ref: 'goods', index: true},
    followUser: {type: Schema.Types.ObjectId, ref: 'user', index: true},                  //关注的用户
    dateCreated: {type: Date, default: Date.now, index: true},
    fromCommunity: {type: Schema.Types.ObjectId, ref: 'community'},
    toCommunity: {type: Schema.Types.ObjectId, ref: 'community'}
});
mongoose.model('user_followups', userFollowupSchema, 'user_followups');


//===========================用户认证 user followup ========================================
var userAuthenticationSchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    imageURL: String,
    promotionCode: String,
    status: {type: Boolean, default: false},
    dateCreated: {type: Date, default: Date.now},
    dateLastModified: {type: Date, default: Date.now},
    isValid: {type: Boolean, default: true},
    refusalReason: String
});
mongoose.model('user_authentications', userAuthenticationSchema, 'user_authentications');


//===========================用户小区 user community ========================================
var userCommunitySchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    type: {type: Number, default: 1},              //1,自己小区  2,父母小区
    userRole: [{type: Number}],
    roomUnit: String,
    roomNumber: String,
    dateCreated: {type: Date, default: Date.now},
    dateLastModified: {type: Date, default: Date.now}
});
mongoose.model('user_community', userCommunitySchema, 'user_community');

var inviteWechatBindSchema = new Schema({
    inviteCode: String,
    unionId: String,
    fromUserId: String
});
mongoose.model('invite_wechat_bind', inviteWechatBindSchema, 'invite_wechat_bind');

var inviteScoreRecord = new Schema({
    user: String,
    invited: String,
    source: Number,                 //积分来源
    score: Number,
    reason: String,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('invite_score_record', inviteScoreRecord, 'invite_score_record');

var recentContactPerson = new Schema({
    userHuanxin: String,
    otherHuanxin: String,
    time: Date
});
mongoose.model('user_recentContactPerson', recentContactPerson, 'user_recentContactPerson');

var iWantSchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    type: Number,                                           //1,找玩伴; 2,干点啥;3,找老乡
    condition: String,
    content: [String],                                      //所想内容
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('user_i_want', iWantSchema, 'user_i_want');

var roleApply = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    city: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},
    type: Number,                                            //1,申请管理员  2,申请雷锋团  4,创始人
    name: String,
    phone: String,
    address: String,
    specialty: [{type: String}],
    status: {type: Number, default: 0},                                  //0,审核中 1,通过 -1,未通过
    startDateTime:{type: Date}, // 通过大使申请日时
    endDateTime:{type: Date}, // 取消拒绝大使申请日时
    isValid: {type: Boolean, default: true},
    dateCreated: {type: Date, default: Date.now},
    /* 宝宝树66小区 sprint5 创始人申请页面 取消大使修改 modify wangcan 20171207 start */
    cancelReason: String  // 取消大使或拒绝创始人申请原因
    /* 宝宝树66小区 sprint5 创始人申请页面 取消大使修改 modify wangcan 20171207 end */
});
mongoose.model('user_role_apply', roleApply, 'user_role_apply');

var userLbs = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    lat: Number,
    lng: Number,
    address: String,
    deviceId: String,
    vName: String,
    vCode: String,
    os: String,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('user_lbs_record', userLbs, 'user_lbs_record');
/*宝宝树66小区二次改造    add   chenf  20170815   start 恢复签到功能*/
var signInfo = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    signDate: {type: Date,default: Date.now},
    isValid: {type: Boolean, default: true},
    /*宝宝树66小区二次改造    add   guo.xiaomin  20170830   start */
    isLuckDraw: {type: Boolean, default: false}, // 是否抽奖
    /*宝宝树66小区二次改造    add   guo.xiaomin  20170830   end */
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('sign_info', signInfo, 'sign_info');
/*宝宝树66小区二次改造    add   chenf  20170815   end */
